<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book reacjs开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>react路由 | reacjs开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../005day/section02.html" />
    
    
    <link rel="prev" href="../005day/index.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="5.1"
        data-chapter-title="react路由"
        data-filepath="005day/section01.md"
        data-basepath=".."
        data-revision="Sun Feb 16 2020 11:31:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="001day/index.html">
            
                
                    <a href="../001day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        react入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="001day/section01.html">
            
                
                    <a href="../001day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        react介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="001day/section02.html">
            
                
                    <a href="../001day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        快速开始
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="001day/section03.html">
            
                
                    <a href="../001day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        JSX语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="001day/section04.html">
            
                
                    <a href="../001day/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        函数组件和参数props
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="001day/section05.html">
            
                
                    <a href="../001day/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        ES6语法之类和类的继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="001day/section06.html">
            
                
                    <a href="../001day/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        类组件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="001day/section07.html">
            
                
                    <a href="../001day/section07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        绑定事件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="001day/section08.html">
            
                
                    <a href="../001day/section08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        状态(state)
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="002day/index.html">
            
                
                    <a href="../002day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        react进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="002day/section01.html">
            
                
                    <a href="../002day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        新增数组方法、属性表达式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="002day/section02.html">
            
                
                    <a href="../002day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        列表渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="002day/section03.html">
            
                
                    <a href="../002day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        条件渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="002day/section04.html">
            
                
                    <a href="../002day/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        表单数据绑定
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="002day/section05.html">
            
                
                    <a href="../002day/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        Refs操作元素，文件上传
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="002day/section06.html">
            
                
                    <a href="../002day/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        生命周期方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="003day/index.html">
            
                
                    <a href="../003day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        脚手架开发
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="003day/section01.html">
            
                
                    <a href="../003day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        yarn的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="003day/section02.html">
            
                
                    <a href="../003day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        脚手架开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="004day/index.html">
            
                
                    <a href="../004day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        react高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="004day/section01.html">
            
                
                    <a href="../004day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        js模块化开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="004day/section02.html">
            
                
                    <a href="../004day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据传递
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="004day/section03.html">
            
                
                    <a href="../004day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        拆分组件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="004day/section04.html">
            
                
                    <a href="../004day/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        组件children属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="004day/section05.html">
            
                
                    <a href="../004day/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        props校验
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="004day/section06.html">
            
                
                    <a href="../004day/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        props默认值
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="004day/section07.html">
            
                
                    <a href="../004day/section07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        render-props模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="004day/section08.html">
            
                
                    <a href="../004day/section08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        高阶组件(HOC)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="004day/section09.html">
            
                
                    <a href="../004day/section09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组件更新机制及性能优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="004day/section10.html">
            
                
                    <a href="../004day/section10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.10.</b>
                        
                        虚拟dom及diff算法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="005day/index.html">
            
                
                    <a href="../005day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        react深入
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="5.1" data-path="005day/section01.html">
            
                
                    <a href="../005day/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        react路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="005day/section02.html">
            
                
                    <a href="../005day/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="005day/section03.html">
            
                
                    <a href="../005day/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        react-redux
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >reacjs开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="react&#x8DEF;&#x7531;">React&#x8DEF;&#x7531;</h2>
<p>&#x8DEF;&#x7531;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x8BBF;&#x95EE;&#x5730;&#x5740;&#x7684;&#x4E0D;&#x540C;&#xFF0C;&#x8BA9;&#x5355;&#x9875;&#x9762;&#x7684;react&#x7CFB;&#x7EDF;&#x5728;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x4E0A;&#x52A0;&#x8F7D;&#x4E0D;&#x540C;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x4ECE;&#x800C;&#x5B9E;&#x73B0;&#x7CFB;&#x7EDF;&#x529F;&#x80FD;&#x7684;&#x5207;&#x6362;&#x7684;&#xFF0C;react&#x662F;&#x901A;&#x8FC7;react-router-dom&#x6A21;&#x5757;&#x6765;&#x5B9E;&#x73B0;&#x8DEF;&#x7531;&#x7684;&#x3002;</p>
<h4 id="&#x57FA;&#x672C;&#x7528;&#x6CD5;">&#x57FA;&#x672C;&#x7528;&#x6CD5;</h4>
<p>&#x9996;&#x5148;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;&#xFF1A;</p>
<pre><code>yarn add react-router-dom
</code></pre><p>&#x63A5;&#x7740;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x5BFC;&#x5165;&#x5BF9;&#x5E94;&#x7684;&#x4F9D;&#x8D56;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {HashRouter,Link,Route,Switch,Redirect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>
</code></pre>
<p>&#x5176;&#x4E2D;&#xFF1A;</p>
<ul>
<li>HashRoter &#x5B9A;&#x4E49;&#x54C8;&#x5E0C;&#x8DEF;&#x7531;&#x6574;&#x4F53;&#x7684;&#x5BB9;&#x5668;&#x6807;&#x7B7E;</li>
<li>Link &#x5355;&#x4E2A;&#x6807;&#x7B7E;&#xFF0C;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x7684;&#x94FE;&#x63A5;&#xFF0C;&#x901A;&#x8FC7;&#x201C;to&#x201D;&#x5C5E;&#x6027;&#x6765;&#x5B9A;&#x4E49;&#x94FE;&#x63A5;&#x5730;&#x5740;</li>
<li>Route &#x5355;&#x4E2A;&#x6807;&#x7B7E;&#xFF0C;&#x5B9A;&#x4E49;&#x7EC4;&#x4EF6;&#x7684;&#x5BB9;&#x5668;&#x6807;&#x7B7E;&#xFF0C;&#x901A;&#x8FC7;&#x201C;path&#x201D;&#x5B9A;&#x4E49;&#x548C;Link&#x7684;to&#x5C5E;&#x6027;&#x5BF9;&#x5E94;&#x7684;&#x5730;&#x5740;&#xFF0C;component&#x5C5E;&#x6027;&#x5B9A;&#x4E49;&#x94FE;&#x63A5;&#x5BF9;&#x5E94;&#x7684;&#x7EC4;&#x4EF6;</li>
<li>Switch &#x591A;&#x4E2A;Route&#x6807;&#x7B7E;&#x5916;&#x9762;&#x7684;&#x5BB9;&#x5668;&#x6807;&#x7B7E;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x5B9A;&#x4E49;404&#x8DF3;&#x8F6C;&#x548C;&#x91CD;&#x5B9A;&#x5411;&#x8DF3;&#x8F6C;&#xFF0C;&#x9700;&#x8981;&#x7528;&#x6B64;&#x6807;&#x7B7E;&#x5305;&#x88F9;Route&#x6807;&#x7B7E;</li>
<li>Redirect &#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#x91CD;&#x5B9A;&#x5411;&#xFF0C;&#x901A;&#x8FC7;&#x201C;from&#x201D;&#x5C5E;&#x6027;&#x5B9A;&#x4E49;&#x539F;&#x59CB;&#x8DEF;&#x7531;&#xFF0C;&#x901A;&#x8FC7;&#x201C;to&#x201D;&#x5C5E;&#x6027;&#x5B9A;&#x4E49;&#x91CD;&#x5B9A;&#x5411;&#x8DEF;&#x7531;</li>
</ul>
<p>&#x8DEF;&#x7531;&#x7684;&#x529F;&#x80FD;&#x76F4;&#x63A5;&#x5B9A;&#x4E49;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#xFF0C;&#x4E0B;&#x9762;&#x662F;&#x8DEF;&#x7531;&#x57FA;&#x672C;&#x7528;&#x6CD5;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x5B50;&#x7EC4;&#x4EF6;01</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Page01</span>(<span class="hljs-params">props</span>)</span>{
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>&#x9875;&#x9762;&#x5185;&#x5BB9;&#x4E00;<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
}

// &#x5B9A;&#x4E49;&#x5B50;&#x7EC4;&#x4EF6;02
function Page02(props){
    return <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>&#x9875;&#x9762;&#x5185;&#x5BB9;&#x4E8C;<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
}

// &#x5B9A;&#x4E49;&#x5B50;&#x7EC4;&#x4EF6;03
function Page03(props){
    return (
        <span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>&#x9875;&#x9762;&#x5185;&#x5BB9;&#x4E09;<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">ul</span>&gt;</span>
                {/* &#x94FE;&#x63A5;&#x5730;&#x5740;&#x4F20;&#x9012;&#x53C2;&#x6570; */}
                <span class="hljs-tag">&lt;<span class="hljs-title">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;#/page03/detail/1001&quot;</span>&gt;</span>&#x65B0;&#x95FB;&#x6807;&#x9898;&#x4E00;<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;#/page03/detail/1002&quot;</span>&gt;</span>&#x65B0;&#x95FB;&#x6807;&#x9898;&#x4E8C;<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;#/page03/detail/1003&quot;</span>&gt;</span>&#x65B0;&#x95FB;&#x6807;&#x9898;&#x4E09;<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">li</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">ul</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>

    )</span> 
}


<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x8D1F;&#x8D23;&#x8DEF;&#x7531;&#x7684;&#x7EC4;&#x4EF6;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Myrouter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render(){
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">HashRouter</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-title">Link</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/&quot;</span>&gt;</span>&#x9875;&#x9762;&#x4E00;<span class="hljs-tag">&lt;/<span class="hljs-title">Link</span>&gt;</span> &amp;nbsp; &amp;nbsp;
                    <span class="hljs-tag">&lt;<span class="hljs-title">Link</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/page02&quot;</span>&gt;</span>&#x9875;&#x9762;&#x4E8C;<span class="hljs-tag">&lt;/<span class="hljs-title">Link</span>&gt;</span> &amp;nbsp; &amp;nbsp;
                    <span class="hljs-tag">&lt;<span class="hljs-title">Link</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/page03&quot;</span>&gt;</span>&#x9875;&#x9762;&#x4E09;<span class="hljs-tag">&lt;/<span class="hljs-title">Link</span>&gt;</span>                      
                <span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">hr</span> /&gt;</span>

                {/* &#x52A0;&#x4E0A;exact&#x662F;&#x4E3A;&#x4E86;&#x8BA9;path&#x7CBE;&#x786E;&#x5339;&#x914D;&#xFF0C;&#x5426;&#x5219;&#x201C;/&#x201D;&#x548C;&#x201C;/china&#x201D;&#x7684;&#x8DEF;&#x5F84;&#x4F1A;&#x540C;&#x65F6;&#x5339;&#x914D; */}
                <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">Page01</span> } /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/page02&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">Page02</span> } /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/page03&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">Page03</span> } /&gt;</span>          
            <span class="hljs-tag">&lt;/<span class="hljs-title">HashRouter</span>&gt;</span>
        )</span>
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Myrouter;
</code></pre>
<h4 id="&#x8DEF;&#x7531;&#x4F20;&#x53C2;">&#x8DEF;&#x7531;&#x4F20;&#x53C2;</h4>
<p>&#x8DEF;&#x7531;&#x4F20;&#x53C2;&#x6307;&#x7684;&#x662F;&#x8DEF;&#x7531;&#x643A;&#x5E26;&#x53C2;&#x6570;&#xFF0C;&#x8DEF;&#x7531;&#x4F20;&#x53C2;&#x53EF;&#x4EE5;&#x7528;params&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x9996;&#x5148;&#x662F;Link&#x6807;&#x7B7E;&#x7684;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-javascript">&lt;li&gt;<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;#/page03/detail/1001&quot;</span>&gt;</span>&#x65B0;&#x95FB;&#x6807;&#x9898;&#x4E00;<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-title">li</span>&gt;</span>
</span></code></pre>
<p>&#x63A5;&#x7740;&#x662F;Route&#x6807;&#x7B7E;&#x7684;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-javascript">&lt;Route path=<span class="hljs-string">&quot;/page03/detail/:newsid&quot;</span> component={ Detail } /&gt;
</code></pre>
<p>&#x7EC4;&#x4EF6;&#x4E2D;&#x83B7;&#x53D6;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x4F7F;&#x7528;props&#x5C5E;&#x6027;&#x83B7;&#x53D6;&#x8DEF;&#x7531;&#x4F20;&#x9012;&#x8FC7;&#x6765;&#x7684;&#x53C2;&#x6570;&#xFF1A;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Detail</span>(<span class="hljs-params">props</span>)</span>{
    <span class="hljs-comment">// &#x901A;&#x8FC7; props.match.params.newsid &#x6765;&#x63A5;&#x6536;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;</span>
    <span class="hljs-comment">//console.log(props.match.params.newsid);</span>
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">p</span>&gt;</span>&#x8FD9;&#x662F;&#x65B0;&#x95FB;&#x7684;&#x8BE6;&#x60C5;&#x9875;&#x9762;,&#x65B0;&#x95FB;&#x7684;ID&#x662F;{ props.match.params.newsid }<span class="hljs-tag">&lt;/<span class="hljs-title">p</span>&gt;</span>
}
</span></code></pre>
<h4 id="404&#x9875;&#x9762;&#x53CA;&#x91CD;&#x5B9A;&#x5411;">404&#x9875;&#x9762;&#x53CA;&#x91CD;&#x5B9A;&#x5411;</h4>
<p>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;404&#x5BF9;&#x5E94;&#x7684;&#x7EC4;&#x4EF6;NotFound&#xFF0C;Route&#x914D;&#x7F6E;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-javascript">&lt;Switch&gt;
    {<span class="hljs-comment">/* &#x52A0;&#x4E0A;exact&#x662F;&#x4E3A;&#x4E86;&#x8BA9;path&#x7CBE;&#x786E;&#x5339;&#x914D;&#xFF0C;&#x5426;&#x5219;&#x201C;/&#x201D;&#x548C;&#x201C;/china&#x201D;&#x7684;&#x8DEF;&#x5F84;&#x4F1A;&#x540C;&#x65F6;&#x5339;&#x914D; */</span>}
    {<span class="hljs-comment">/* &lt;Route exact path=&quot;/&quot; component={page01} /&gt; */</span>}
    &lt;Route path=<span class="hljs-string">&quot;/page01&quot;</span> component={ Page01 } /&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/page02&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">Page02</span> } /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/page03&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">Page03</span> } /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/page03/detail/:newsid&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">Detail</span> } /&gt;</span>

    {/* &#x91CD;&#x5B9A;&#x5411;&#x8981;&#x5199;&#x5728;&#x8DF3;&#x8F6C;&#x8DEF;&#x7531;&#x7684;&#x4E0B;&#x9762; */}
    <span class="hljs-tag">&lt;<span class="hljs-title">Redirect</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">from</span>=<span class="hljs-value">&quot;/&quot;</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/page01&quot;</span> /&gt;</span>

    {/* 404&#x7684;&#x8DEF;&#x7531;&#x9700;&#x8981;&#x5199;&#x5728;&#x6700;&#x4E0B;&#x9762; */}
    <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{NotFound}</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">Switch</span>&gt;</span>
</span></code></pre>
<h4 id="&#x81EA;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;">&#x81EA;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;</h4>
<p>&#x6709;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x9875;&#x9762;&#x83DC;&#x5355;&#x80FD;&#x6307;&#x793A;&#x5F53;&#x524D;&#x9875;&#x9762;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x7528;&#x5230;&#x81EA;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">CustomLink</span>(<span class="hljs-params">{label,to,exact}</span>)</span>{
    <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">Route</span>
        <span class="hljs-attribute">path</span> = { <span class="hljs-attribute">to</span> }
        <span class="hljs-attribute">exact</span> = { <span class="hljs-attribute">exact</span> }
        // <span class="hljs-attribute">match</span>&#x53C2;&#x6570;&#x662F;&#x7CFB;&#x7EDF;&#x4F20;&#x5165;&#x7684;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x5339;&#x914D;&#x5F53;&#x524D;&#x8DEF;&#x7531;&#xFF0C;&#x5C31;&#x662F;<span class="hljs-attribute">true</span>
        // &#x4E0D;&#x5339;&#x914D;&#x5F53;&#x524D;&#x8DEF;&#x7531;&#xFF0C;&#x5C31;&#x662F;<span class="hljs-attribute">false</span>
        <span class="hljs-attribute">children</span> = {({<span class="hljs-attribute">match</span>})=&gt;</span>(
            <span class="hljs-tag">&lt;<span class="hljs-title">Link</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">to</span> } <span class="hljs-attribute">className</span>=<span class="hljs-value">{</span> <span class="hljs-attribute">match</span>?&apos;<span class="hljs-attribute">active</span>&apos;<span class="hljs-attribute">:</span>&apos;&apos;}&gt;</span>{ label }<span class="hljs-tag">&lt;/<span class="hljs-title">Link</span>&gt;</span>
        )</span>}    
    /&gt;)
}
</code></pre>
<p>&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x8DEF;&#x7531;&#xFF1A;  </p>
<pre><code class="lang-javascript">&lt;CustomLink label=<span class="hljs-string">&quot;&#x9875;&#x9762;&#x4E00;&quot;</span> to=<span class="hljs-string">&quot;/&quot;</span> exact={ <span class="hljs-literal">true</span> } /&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">CustomLink</span> <span class="hljs-attribute">label</span>=<span class="hljs-value">&quot;&#x9875;&#x9762;&#x4E8C;&quot;</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/page02&quot;</span>  /&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">CustomLink</span> <span class="hljs-attribute">label</span>=<span class="hljs-value">&quot;&#x9875;&#x9762;&#x4E09;&quot;</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/page03&quot;</span>  /&gt;</span>
</span></code></pre>
<h4 id="&#x5B50;&#x8DEF;&#x7531;">&#x5B50;&#x8DEF;&#x7531;</h4>
<p>react&#x8DEF;&#x7531;&#x662F;&#x76F4;&#x63A5;&#x5199;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x5B50;&#x8DEF;&#x7531;&#x5C31;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5199;&#x5728;&#x8DF3;&#x8F6C;&#x7684;&#x7EC4;&#x4EF6;&#x4E2D;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x53EF;&#x4EE5;&#x4E0D;&#x5199;HashRouter&#x5BB9;&#x5668;&#x6807;&#x7B7E;&#xFF0C;&#x6709;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x5728;&#x903B;&#x8F91;&#x4EE3;&#x7801;&#x4E2D;&#x8DF3;&#x8F6C;&#x8DEF;&#x7531;&#xFF0C;&#x8FD9;&#x4E2A;&#x53EB;&#x505A;&#x7F16;&#x7A0B;&#x5F0F;&#x5BFC;&#x822A;&#xFF0C;&#x8DF3;&#x8F6C;&#x7684;&#x65B9;&#x5F0F;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">this</span>.props.history.push(<span class="hljs-string">&apos;/layout&apos;</span>);
</code></pre>
<p>&#x5B50;&#x8DEF;&#x7531;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> { HashRouter, Link, Route, Switch, Redirect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>


class Login extends Component {
    constructor(props) {
        super(props)
    }
    fnJump = () =&gt; {
        this.props.history.push(<span class="hljs-string">&apos;/layout&apos;</span>);
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">br</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;password&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">br</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">&quot;&#x767B;&#x5F55;&quot;</span> <span class="hljs-attribute">onClick</span>=<span class="hljs-value">{this.fnJump}</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
        )</span>
    }
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Menu01</span>(<span class="hljs-params">props</span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>&#x7BA1;&#x7406;&#x9875;&#x9762;&#x5185;&#x5BB9;&#x4E00;<span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    )</span>
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Menu02</span>(<span class="hljs-params">props</span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>&#x7BA1;&#x7406;&#x9875;&#x9762;&#x5185;&#x5BB9;&#x4E8C;<span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    )</span>
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Layout</span>(<span class="hljs-params">props</span>) </span>{
    <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Link</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/layout/menu01&quot;</span>&gt;</span>&#x83DC;&#x5355;&#x4E00;<span class="hljs-tag">&lt;/<span class="hljs-title">Link</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Link</span> <span class="hljs-attribute">to</span>=<span class="hljs-value">&quot;/layout/menu02&quot;</span>&gt;</span>&#x83DC;&#x5355;&#x4E8C;<span class="hljs-tag">&lt;/<span class="hljs-title">Link</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">hr</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/layout&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{Menu01}</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/layout/menu01&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{Menu01}</span> /&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/layout/menu02&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{Menu02}</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    )</span>
}


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Myrouter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">HashRouter</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">exact</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{Login}</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/login&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{Login}</span> /&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">Route</span> <span class="hljs-attribute">path</span>=<span class="hljs-value">&quot;/layout&quot;</span> <span class="hljs-attribute">component</span>=<span class="hljs-value">{Layout}</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">HashRouter</span>&gt;</span>
        )</span>
    }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Myrouter;
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../005day/index.html" class="navigation navigation-prev " aria-label="Previous page: react深入"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../005day/section02.html" class="navigation navigation-next " aria-label="Next page: redux"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/fontsettings.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book reacjs开发 -->
</html>
